package it.polito.let.http;

import java.util.*;

public class Header {

	private String token;
	private HashMap<String, String> parameters = new HashMap<String, String>();

	public Header(String token) {
		this.token = token;
	}

	@Override
	public String toString() {
		StringBuffer out = new StringBuffer(token);

		for(String key : parameters.keySet()) {
			out.append(";");
			out.append(key);
			out.append("=");
			out.append(parameters.get(key));
		}
		return out.toString();
	}

	public String getToken() {
		return token;
	}

	public void setParameter(String attribute, String value) {
		this.parameters.put(attribute, value);
	}

	public boolean containsAttribute(String attribute) {
		return parameters.containsKey(attribute);
	}

	public String getAttribute(String attribute) {
		return parameters.get(attribute);
	}

	public static Header parseHeader(String headerValue) throws HTTPError {
		StringTokenizer tokenizer = new StringTokenizer(headerValue, ";");
		String token = tokenizer.nextToken().trim();
		Header parsedHeaderValue = new Header(token);

		while(tokenizer.hasMoreTokens()) {
			String headerToken = tokenizer.nextToken();
			int delim = headerToken.indexOf('=');
			if(delim == -1) throw new HTTPError((short)400, "bad request");
			String attribute = headerToken.substring(0, delim).trim();
			String value = headerToken.substring(delim + 1).trim();
			if(value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"')
				value = value.substring(1, value.length() - 1);
			parsedHeaderValue.setParameter(attribute, value);
		}

		return parsedHeaderValue;
	}
}
